﻿using System;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace VitCode.Web
{
    public partial class Default : VcPage 
    {
 
        private CastleAccess m_CastleAccess = new CastleAccess();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            
            // if (!Global.m_IsFirstLogin)
            //{
            //    var computerLoginName = WindowsIdentity.GetCurrent(false).Name;
            //    var nscsService = new NscsService();
            //    var userObj = nscsService.CheckAotuLogin(computerLoginName);
            //    if (userObj != null && userObj.HasRole)
            //    {
            //        var strUserRolesInfo = m_CastleAccess.GetUserRolesStringByUserId(userObj.Id.ToString());
            //        var m_LogManger = new SystemLogManager();
            //        var areaInfoList = m_CastleAccess.GetObjectList(new AreaInfo().GetType().Name);
            //        if (areaInfoList != null && areaInfoList.Count > 0)
            //        {
            //            Global.m_IsFirstLogin = true;
            //            var currentAreaInfo = (AreaInfo)areaInfoList[0];
            //            m_LogManger.SaveSystemLogInfo(userObj.Code, userObj.Name, "Login", "NSCSWeb", currentAreaInfo.Id);
            //            GenericContext.CurrAreaId = currentAreaInfo.Id.ToString();

            //            GenericContext.CurrUserCode = userObj.Code;
            //            GenericContext.CurrUserId = userObj.Id;
            //            GenericContext.CurrUserName = userObj.Name;
            //            GenericContext.UserRolesString = strUserRolesInfo;
            //            const string strURL = "MainWebForm/Main.aspx";
            //            Response.Redirect(strURL);
            //        }
            //    }
            //}
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            string strCode = publ.GetCatchMsg(UserName.Value.Trim());
            string strPassword = publ.GetCatchMsg(Password.Value.Trim());
            strPassword = publ.VitEnctyptString(strPassword);

            var objCurUser = m_CastleAccess.CheckLogin(strCode, strPassword);
            if (objCurUser != null)
            {
                var strUserRolesInfo = m_CastleAccess.GetUserRolesStringByUserId(objCurUser.Id.ToString());
                var m_LogManger = new SystemLogManager();
                GenericContext.CurrUserCode = strCode;
                GenericContext.CurrUser = objCurUser;
                GenericContext.CurrUserId = objCurUser.Id;
                GenericContext.CurrUserName = objCurUser.Name;
                GenericContext.UserRolesString = strUserRolesInfo;
                const string strURL = "MainWebForm/Main.aspx";
                Response.Redirect(strURL);
            }
            else
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "001", "<script>alert('密碼錯誤!')</script>");
            }
        }
    }
}
